import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpResponse } from '@angular/common/http';
import { Subscription } from 'rxjs/Subscription';
import { JhiEventManager, JhiDataUtils } from 'ng-jhipster';

import { BookBasic } from './book-basic.model';
import { BookBasicService } from './book-basic.service';
import {Principal} from '../../shared';
import {ShoppingCart, ShoppingCartService} from '../shopping-cart';
import { Router } from '@angular/router';
import {CommentService} from "../comment/comment.service";
import { Comment } from '../comment/comment.model';

@Component({
    selector: 'jhi-book-basic-detail',
    templateUrl: './book-basic-detail.component.html'
})
export class BookBasicDetailComponent implements OnInit, OnDestroy {
    account: any;
    inputValue: '';
    bookBasic: BookBasic;
    shopcartamount: Number;

    comment= new Array();
    savecomment: Comment;
    currentbookid:any;

    private subscription: Subscription;
    private eventSubscriber: Subscription;


    constructor(
        private router:Router,
        private shopCartService: ShoppingCartService,
        private principal: Principal,
        private eventManager: JhiEventManager,
        private dataUtils: JhiDataUtils,
        private bookBasicService: BookBasicService,
        private route: ActivatedRoute,
        private commentService: CommentService
    ) {
    }

    addtoshopcart(book:BookBasic){
        if(!this.account||!this.account.id){
            return;
        }
        this.shopCartService.addShoppingCart(book, this.account, this.shopcartamount).subscribe(result=>{
           console.log(result);
           if(result.status===201){
               this.router.navigate(['shopping-cart']);
           }
        },error => {

        });


    }

    ngOnInit(){
        this.principal.identity().then((account) => {
            this.account = account;
            console.log("book-detail-account",account);
        });

        this.subscription = this.route.params.subscribe((params) => {
            this.currentbookid=params['id'];
            this.load(params['id']);
            this.findCommmentsByBookId();

        });
        this.registerChangeInBookBasics();

    }

    load(id) {
        this.bookBasicService.find(id)
            .subscribe((bookBasicResponse: HttpResponse<BookBasic>) => {
                this.bookBasic = bookBasicResponse.body;
            });
    }
    byteSize(field) {
        return this.dataUtils.byteSize(field);
    }
    save() {
        console.log("savecomment Before", this.account);
        this.savecomment = new Comment();
        this.savecomment.commentator = this.account.firstName;
        this.savecomment.content = this.inputValue;
        this.savecomment.bookId = this.bookBasic;
        this.commentService.postCommments(this.savecomment).subscribe( (e) =>  {
            console.log("savecomment Before",e);
            this.inputValue="";
           this.findCommmentsByBookId();
        });
    }
    openFile(contentType, field) {
        return this.dataUtils.openFile(contentType, field);
    }
    previousState() {
        window.history.back();
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
        this.eventManager.destroy(this.eventSubscriber);
    }
    findCommmentsByBookId( ) {
     this.commentService.findCommmentsByBookId(this.currentbookid).subscribe(
         (data) => {
             if (!this.currentbookid) {

             }
             this.comment = data;
             /*for (let i = 0; i < this.comment.length; i++)
                 this.comment[i].commentTime = this.comment[i].commentTime.substr(0, 10);*/
         },
         (error) => {
             console.log(error);
         }
     );
    }
    registerChangeInBookBasics(){
        this.eventSubscriber = this.eventManager.subscribe(
            'bookBasicListModification',
            (response) => this.load(this.bookBasic.id)
        );
    }

}
